package com.ruoyi.system.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SurveyAnswer;
import com.ruoyi.system.service.SurveyAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/surveyAnswer")
public class SurveyAnswerController extends BaseController {

    @Autowired
    private SurveyAnswerService surveyAnswerService;

    /**
     * 查询答题记录列表
     * userId = null → 管理员查所有
     * userId != null → 查指定用户
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(required = false) Long userId) {
        startPage();
        List<SurveyAnswer> list;
        if (userId != null) {
            list = surveyAnswerService.selectSurveyAnswerByUserId(userId);
        } else {
            list = surveyAnswerService.selectSurveyAnswerList(new SurveyAnswer());
        }
        return getDataTable(list);
    }

    /**
     * 新增答题
     */
    @PostMapping
    public AjaxResult add(@RequestBody SurveyAnswer surveyAnswer) {
        return toAjax(surveyAnswerService.insertSurveyAnswer(surveyAnswer));
    }
}
